import grace from "../../../utils/grace.js";
import {Product} from '../product/product-model.js';

let product = new Product();

grace.page({
  /**
   * 页面的初始数据
   */
  data: {
    banners: [],
    products: [],
    total: 0,
    page: 1,
    pagesize: 20
  },
  onLoad() {
    // 查询首页banner
    index.getIndexBanners({'location': 'group_index'}, (data) => {
      if(data.code == 0) {
        this.$data.banners = data.data;
      }
    })

    this.getProducts();
  },
  getProducts() {
    // 查询团购产品
    let params = {
      'is_group': true,
      'page': this.$data.page,
      'pagesize': this.$data.pagesize
    }

    product.getProductList(params, (data) => {
      if (data.code == 0) {
        this.$data.total = data.data.total;
        this.$data.page = this.$data.page + 1;
        this.$data.products = this.$data.products.concat(data.data.list);
      }
    })
  },
  onReachBottom() {
    if (this.$data.products.length < this.$data.total) {
      this.getProducts();
    }
  }
})